Objected-oriented programs commonly operate in computers and computer-embedded devices to provide a variety of functions. These programs may be created using object-oriented programming languages such as Java™, C++, and many others. (Java and all Java-based trademarks and logos are trademarks or registered trademarks of Oracle and/or its affiliates.) An object-oriented program operates by manipulating data in the form of objects. An object refers to a real-world object or concept, for example, a bicycle or a bank account. An object-oriented program may be viewed as a collection of cooperating objects.
In object-oriented programming, an object is created when a “class” that defines a particular type of objects is instantiated. An object is thus a particular instance of a class. A class provides the characteristics of its objects, their behavior and abilities. In terms of object characteristics, the class includes the object's attributes, fields or properties, e.g., the bicycles in the class are racing bicycles. In terms of behavior and abilities, the class includes the functions or methods that the objects in the class may perform or behave. For example, a class about bicycles may include methods related to their speed and braking abilities.
A class can have one or more inner classes, which are also known as non-static nested classes. The inner classes have access to private members of the enclosing class and the enclosing class has access to private members of the inner class at compile time. Because the inner classes are compiled into separate class files when a Java program is compiled, a Java virtual machine will enforce the visibility rules for the inner classes just like for any other class. As a result, an inner class cannot access a private member in the enclosing class, and vice versa, at runtime.
To circumvent the above problem, a compiler may insert synthetic accessor methods into the compiled class files to enable an inner class to access the private members of the enclosing class, and vice versa. The synthetic accessor methods that were added by the compiler have the default visibility of “package-private”. The package-private visibility allows all classes within the same package (e.g., com.ibm.internal) to access the private methods at runtime. As a result, the synthetic accessor methods enable indirect access by an inner class to the private members of the enclosing class, and vice versa.
The added synthetic accessor methods, however, increase the size of the Java classes and require more memory to run on a Java virtual machine. The amount of memory consumed can add up with a large number of classes and synthetic accessor methods (e.g., this can be up to 2 MB for some Java applications). Furthermore, the addition of the synthetic accessor methods has a negative impact on the performance of the Java program because of the extra invocations of the synthetic accessor methods. For example, there are two invocations when an accessed private class member is a private method: one invocation for a synthetic accessor method and another invocation for the private method.